package cn.itsource.pet.service.impl;

import cn.itsource.basic.constant.PetHomeConstans;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.pet.domain.Pet;
import cn.itsource.pet.domain.PetDetail;
import cn.itsource.pet.domain.SearchMasterMsg;
import cn.itsource.pet.mapper.PetDetailMapper;
import cn.itsource.pet.mapper.PetMapper;
import cn.itsource.pet.mapper.SearchMapper;
import cn.itsource.pet.service.IPetService;
import cn.itsource.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private SearchMapper searchMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 处理未处理的寻主宠物
     * @param
     */
    @Override
    @Transactional
    public void insertPending(Pet pet) {
        //System.out.println(pet);
        //设置来源
        pet.setSourceType(PetHomeConstans.SEARCH_MASTER);
        //保存pet
        petMapper.save(pet);
        //保存宠物详情
        PetDetail detail = pet.getDetail();
        detail.setPet(pet);
        petDetailMapper.save(detail);
        //将寻主信息该为已处理
        SearchMasterMsg searchMasterMsg = pet.getSearchMasterMsg();

        searchMapper.upStates(searchMasterMsg.getId());
    }

    /**
     * 宠物上架
     * @param pets
     */
    @Override
    @Transactional
    public void onsale(List<Pet> pets) {
        petMapper.onsale(pets);
    }

    /**
     * 宠物下架
     * @param pets
     */
    @Override
    @Transactional
    public void offsale(List<Pet> pets) {
        petMapper.offsale(pets);
    }

    /**
     * 门户网站宠物详情展示
     * @param id
     * @return
     */
    @Override
    public Pet getPetsById(Long id) {

        return petMapper.getPetById(id);
    }

    /**
     * 添加
     * @param pet
     */
    @Override
    @Transactional
    public void save(Pet pet) {

        //保存pet表
        super.save(pet);
        //保存详情表
        PetDetail detail = pet.getDetail();
        detail.setPet(pet);
        petDetailMapper.save(detail);
    }

    /**
     * 修改
     * @param pet
     */
    @Override
    @Transactional
    public void update(Pet pet) {
        //修改pet表
        super.update(pet);
        //保存详情表
        PetDetail detail = pet.getDetail();
        detail.setPet(pet);
        petDetailMapper.update(detail);
    }
}
